package rmhc.api.fb
{
	import rmhc.api.ApiWrapper;
	import rmhc.api.dataflow.UserInfo;
	import rmhc.api.tasks.BaseApiTask;
	
	public class FbApiTask extends BaseApiTask
	{
		public static const GET:String = "GET";
		public static const POST:String = "POST";
		
		protected var fbapi:FbApi;
		protected var params:Object;
		protected var target:String;
		protected var raw:Object;
		protected var method:String;
		
		public function FbApiTask( wrapper:ApiWrapper , target:String = null , params:Object = null , method:String = GET )
		{
			super(wrapper);
			
			this.target = target;
			this.params = params;
			this.method = method;
			this.fbapi = wrapper as FbApi;
			fbapi.addTask( this );
		}
		
		public function parseData( result:Object , fail:Object = null ):void 
		{
			if ( result != null )
			{
				raw = result;
				onComplete();
			}
			else
			{
				raw = fail;
				onError( fail );
			}
		}
		
		
		protected function parseUserInfo( o:Object ):UserInfo
		{
			var u:UserInfo = new UserInfo();
			u.uid = o.id;
			u.firstname = o.first_name;
			u.lastname = o.last_name;
			if ( o.gender == "male" )
			{
				u.gender = UserInfo.MALE;
			}
			else if ( o.gender == "female" )
			{
				u.gender = UserInfo.FEMALE;
			}
			
			if ( o.picture )
			{
				u.thumbSrc = o.picture;
			}
			return u;
		}
		
		
		public function getTarget():String
		{
			if ( target == null )
			{
				return "/";
			}
			return "/" + target;
		}
		public function getParams():Object
		{
			return params;
		}
		public function setParam( param:String , value:* ):void
		{
			params[param] = value;
		}
		public function getMethod():String
		{
			return method;
		}
		override public function getRawData():Object
		{
			return raw;
		}
	}
}